//
//  ViewController.m
//  UIHomeWork15_网易标题自适应
//
//  Created by tusm on 16/8/3.
//  Copyright © 2016年 tusm. All rights reserved.
//

#import "ViewController.h"
#import "MyCollectionViewCell.h"
#import <AVFoundation/AVFoundation.h>
#import "MyCollectionViewLayOut.h"
#define kCollectionViewReuse @"reuse"
@interface ViewController ()<UICollectionViewDelegate,UICollectionViewDataSource,MyCollectinViewLayOutDelegate>
@property (nonatomic,strong)UICollectionView *myCollectionview;
@property (nonatomic,strong)NSArray *titleArray;
@property (nonatomic,assign)CGFloat itemWidth;

////字体
//@property (nonatomic,strong)UIFont *font;
////UIlabel实际的大小
//@property (nonatomic,assign)CGSize labelSize;
@property (nonatomic,strong)MyCollectionViewLayOut * layOut;
@property (nonatomic, assign) CGFloat height;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    [self createCollectionView];
    self.navigationController.navigationBar.translucent = NO;
    _titleArray = @[@"哎呀忽然回答我", @"我去feud", @"你妹", @"新闻查你", @"影视", @"社会得还", @"精选等你", @"网易号低洼低洼", @"猴急哦你", @"的奇偶额激动", @"街道口而", @"积分多少了", @"吉林省路费", @"据了解", @"家里舒服"];
//    self.font = [UIFont systemFontOfSize:17];
    _height = 44;
}

- (void)createCollectionView{

    _layOut = [[MyCollectionViewLayOut alloc] init];
    _layOut.titleFont = [UIFont systemFontOfSize:30];

    _layOut.delegate = self;
    //label的长度
    self.myCollectionview = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 90) collectionViewLayout:_layOut];
    _myCollectionview.delegate = self;
    _myCollectionview.dataSource =self;
    _myCollectionview.backgroundColor = [UIColor whiteColor];
    
    
    
    [self.view addSubview:_myCollectionview];
    [_myCollectionview registerClass:[MyCollectionViewCell class] forCellWithReuseIdentifier:kCollectionViewReuse];
    


}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{

    return _titleArray.count;

}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{

    MyCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kCollectionViewReuse forIndexPath:indexPath];
    cell.contentView.backgroundColor = [UIColor colorWithRed:arc4random()% 256 /255.0f green:arc4random()%256 /255.0f blue:arc4random()% 256 /255.0f alpha:0.5];
    NSString *str =  _titleArray[indexPath.row];
    cell.label.text = str;
    cell.label.font = _layOut.titleFont;
//    _labelSize = [str sizeWithFont:_font constrainedToSize: lineBreakMode:<#(NSLineBreakMode)#>];
    return cell;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout widthForItemAtIndexPath:(NSIndexPath *)indexPath{

    NSString *str = _titleArray[indexPath.row];
    CGRect boudingRect = [str boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, _height)
                                       options:NSStringDrawingUsesLineFragmentOrigin
                                    attributes:@{NSFontAttributeName : _layOut.titleFont}
                                       context:nil];
    return boudingRect.size.width;
}








- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
